package com.jiang.jixiang.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.gyf.barlibrary.ImmersionBar;
import com.jiang.jixiang.R;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.bean.SellerInfoBean;
import com.jiang.jixiang.databinding.ActivityNearbysellerinfoBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.ws.universal.tools.base.BaseActivity;
import com.ws.universal.tools.base.ManageActivity;
import com.ws.universal.tools.utils.AppUtils;
import com.ws.universal.tools.utils.GsonUtils;
import com.ws.universal.tools.utils.LocationUtils;
import com.ws.universal.tools.utils.LogcatUtils;
import com.ws.universal.tools.utils.StringUtils;
import com.ws.universal.tools.utils.ToastUtils;
import com.ws.universal.tools.utils.glide.GlideUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 商家详情
 */
public class NearbySellerInfoActivity extends BaseActivity implements NetWorkListener {
    private ActivityNearbysellerinfoBinding binding;
    private Intent intent;
    private String id;


    private Bundle bundle;
    private SellerInfoBean sellerInfoBean;

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        //沉浸式
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .statusBarColor(com.ws.universal.tools.R.color.transparent)
                .fitsSystemWindows(false)  //使用该属性必须指定状态栏的颜色，不然状态栏透明，很难看
                .init();
        ManageActivity.putActivity(this.getClass().getSimpleName(), this);
        binding = ActivityNearbysellerinfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        bundle = getIntent().getExtras();
        id = bundle.getString("id");


    }

    @Override
    protected void initView() {
        binding.titleLeftBtn.setOnClickListener(view -> {
            finish();
        });
        //LogcatUtils.e(" isGoogle " + AppUtils.getAppsInfo());

        binding.btnSubmit.setOnClickListener(view -> {

            LogcatUtils.e(" isGoogle " + AppUtils.isAvilible(getContext(), "com.google.android.apps.maps"));
            String locationStr = LocationUtils.getInstance().getLocationAddress(getContext(), sellerInfoBean.getLatitude(), sellerInfoBean.getLongitude());
//            if (CacheUtils.getString(Constants.langCode).equals("ru-ru")) {
//                if (AppUtils.isAvilible(getContext(), "ru.yandex.yandexmaps")) {
//                    Uri gmmIntentUri = Uri.parse(StringUtils.getString("http://ru.yandex.yandexmaps/maps?saddr=", "&daddr=", locationStr, "&mode=w"));
//                    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
//                    mapIntent.setPackage("ru.yandex.yandexmaps");
//                    startActivity(mapIntent);
//                } else {
//                    Uri uri = Uri.parse("market://details?id=ru.yandex.yandexmaps");
//                    intent = new Intent(Intent.ACTION_VIEW, uri);
//                    startActivity(intent);
//                }
//            } else {
//                if (AppUtils.isAvilible(getContext(), "com.google.android.apps.maps")) {
//
//                    Uri gmmIntentUri = Uri.parse(StringUtils.getString("http://maps.google.com/maps?saddr=", "&daddr=", locationStr, "&mode=w"));
//                    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
//                    mapIntent.setPackage("com.google.android.apps.maps");
//                    startActivity(mapIntent);
//
//                } else {
//                    /*Uri gmmIntentUri = Uri.parse(StringUtils.getString("http://maps.google.com/maps?saddr=", "&daddr=", locationStr, "&mode=w"));
//                    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, gmmIntentUri);
//                    startActivity(intent);*/
//                    Uri uri = Uri.parse("market://details?id=com.google.android.apps.maps");
//                    intent = new Intent(Intent.ACTION_VIEW, uri);
//                    startActivity(intent);
//
//
//                }
//            }

            if (AppUtils.isAvilible(getContext(), "com.google.android.apps.maps")) {

                Uri gmmIntentUri = Uri.parse(StringUtils.getString("http://maps.google.com/maps?saddr=", "&daddr=", locationStr, "&mode=w"));
                Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                mapIntent.setPackage("com.google.android.apps.maps");
                startActivity(mapIntent);


            } else {
                    /*Uri gmmIntentUri = Uri.parse(StringUtils.getString("http://maps.google.com/maps?saddr=", "&daddr=", locationStr, "&mode=w"));
                    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, gmmIntentUri);
                    startActivity(intent);*/
                Uri uri = Uri.parse("market://details?id=com.google.android.apps.maps");
                intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);


            }

        });

        loadData();


    }

    private void loadData() {
        showProgressDialog(false);
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        if (getLocation() != null) {
            params.put("longitude", getLocation().getLongitude());
            params.put("latitude", getLocation().getLatitude());
        }
        params.put("device_type", "powerbank");
        okHttpModel.postJson(ApiUrl.sellerInfo_Api, params, ApiUrl.sellerInfo_Api_ID, this);
    }


    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        JSONObject data = jsonData.optJSONObject("data");
        switch (id) {
            case ApiUrl.sellerInfo_Api_ID:
                sellerInfoBean = GsonUtils.parseJObject(data.toString(), SellerInfoBean.class);
                if (sellerInfoBean != null) {
                    binding.tvSellerName.setText(sellerInfoBean.getName());
                    binding.tvSellerAddress.setText(sellerInfoBean.getAddress());
                    binding.btnSubmit.setText(StringUtils.getString(sellerInfoBean.getDistance()));
                    binding.tvHire.setText(StringUtils.getString(sellerInfoBean.getBorrow_num()));
                    binding.tvReturn.setText(StringUtils.getString(sellerInfoBean.getReturn_num()));
                    binding.tvSellerTime.setText(StringUtils.getString(sellerInfoBean.getShop_start()));

                    GlideUtils.CreateImageRound(sellerInfoBean.getLogo(), binding.imgSeller,
                            R.mipmap.icon_defult_nearbyinfo, R.mipmap.icon_defult_nearbyinfo);
                }
                break;
        }


    }

    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        ToastUtils.showShort(commonality.getMessage());
    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.getMessage());
    }

    @Override
    protected void FinishDesTroy() {
        ManageActivity.removeActivity(this.getClass().getSimpleName());
    }
}
